package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 营业额统计
     *
     * @param begin
     * @param end
     * @return
     */
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
       /* //根据开始时间和结束时间获取每天的日期
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
             begin= begin.plusDays(1);
            dateList.add(begin);
        }
        //查询每天的已完成订单的数量
        List<Double> turnover = dateList.stream().map(date -> {
            Map map = new HashMap<>();
            map.put("begin", LocalDateTime.of(date, LocalTime.MIN));
            map.put("end", LocalDateTime.of(date, LocalTime.MAX));
            map.put("status", Orders.COMPLETED);
            Double turnoverSum = orderMapper.sumByMap(map);
            return turnoverSum;
        }).collect(Collectors.toList());*/

        //获取需要统计营业额的天数列表
        List<LocalDate> dateList = begin.datesUntil(end.plusDays(1), Period.ofDays(1)).collect(Collectors.toList());
        //获取每天的营业额集合List
        Map map = new HashMap<>();
        map.put("begin", LocalDateTime.of(begin, LocalTime.MIN));
        map.put("end", LocalDateTime.of(end, LocalTime.MAX));
        map.put("status", Orders.COMPLETED);
        //获取有订单的日期及营业额
        List<TurnoverReportAllVO> turnoverReportAllVOList = orderMapper.sumByMapAll(map);
        //转换成Map集合
        Map<LocalDate, Double> dateDoubleMap = turnoverReportAllVOList.stream().collect(Collectors.toMap(TurnoverReportAllVO::getDate, TurnoverReportAllVO::getTurnover));
        //获取每天的营业额,包括没有订单的
        List<Double> turnover = dateList.stream().map(date -> {
            if (dateDoubleMap.containsKey(date)) return dateDoubleMap.get(date);
            return 0.0;
        }).collect(Collectors.toList());


        //把日期格式的时间转换成string类型的集合,再把集合转换成一个string类型的字符串
        String dateListAll = StringUtils.join(dateList, ",");
        String turnoverList = StringUtils.join(turnover, ",");
        //封装vo
        TurnoverReportVO reportVO = TurnoverReportVO.builder().
                dateList(dateListAll)
                .turnoverList(turnoverList).build();
        return reportVO;
    }

    /**
     * 用户统计
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        //获取需要统计用户的天数列表
        List<LocalDate> dateList = begin.datesUntil(end.plusDays(1), Period.ofDays(1)).collect(Collectors.toList());
        //获取每日新增用户数量
        Map map = new HashMap<>();
        map.put("begin", LocalDateTime.of(begin, LocalTime.MIN));
        map.put("end", LocalDateTime.of(end, LocalTime.MAX));
        List<UserReportAllVO> newUser = orderMapper.sumUserReport(map);
        Map<LocalDate, Integer> dateIntegerMap = newUser.stream().collect(Collectors.toMap(UserReportAllVO::getDate, UserReportAllVO::getNumber));
        //获取每天的新增用户
        List<Integer> newUserList = dateList.stream().map(date -> {
            if (dateIntegerMap.containsKey(date)) return dateIntegerMap.get(date);
            return 0;
        }).collect(Collectors.toList());
        //获取给定日期前的总的用户
        Integer totalUser = orderMapper.getTotalUser(begin);
        //获取每天的总用户
        List<Integer> totalUserList = new ArrayList<>();
       for (Integer integer : newUserList) {
            totalUser += integer;
            totalUserList.add(totalUser);
        }


        /* //每天总的用户
        List<Integer> totalUserList = new ArrayList<>();
        //每天新增的用户
        List<Integer> newUserList = dateList.stream().map(date -> {
            Map map = new HashMap<>();
            map.put("end", LocalDateTime.of(date, LocalTime.MAX));
            Integer userAll = orderMapper.sumUserByMap(map);
            totalUserList.add(userAll);
            map.put("begin", LocalDateTime.of(date, LocalTime.MIN));
            Integer newUser = orderMapper.sumUserByMap(map);
            return newUser;
        }).collect(Collectors.toList());*/

        //封装vo
        String dateListAll = StringUtils.join(dateList, ",");
        String allUserList = StringUtils.join(totalUserList, ",");
        String newUserAllList = StringUtils.join(newUserList, ",");
        UserReportVO reportVO = UserReportVO.builder().dateList(dateListAll).totalUserList(allUserList).newUserList(newUserAllList).build();
        return reportVO;
    }

    /**
     * 订单统计
     *
     * @param begin
     * @param end
     * @return
     */
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        //根据开始时间和结束时间获取每天的日期
        List<LocalDate> dateList = begin.datesUntil(end.plusDays(1), Period.ofDays(1)).collect(Collectors.toList());
        //每天订单总数集合
        List<Integer> totalOrderList = new ArrayList<>();
        //每天已完成订单总数

        List<Integer> finshOrderList = dateList.stream().map(date -> {
            Map map = new HashMap<>();
            map.put("begin", LocalDateTime.of(date, LocalTime.MIN));
            map.put("end", LocalDateTime.of(date, LocalTime.MAX));
            Integer allOrder = orderMapper.sumOrderByMap(map);
            totalOrderList.add(allOrder);
            map.put("status", Orders.COMPLETED);
            Integer finshOrder = orderMapper.sumOrderByMap(map);
            return finshOrder;
        }).collect(Collectors.toList());


        //有效的和总的订单数
        Integer total = totalOrderList.stream().reduce(Integer::sum).get();
        Integer finsh = finshOrderList.stream().reduce(Integer::sum).get();
        //订单完成率
        Double completion = 0.0;
        if (total != 0) {
            completion = Double.valueOf(finsh) / total;
        }
        //封装VO
        String dateListAll = StringUtils.join(dateList, ",");
        String allOrderList = StringUtils.join(totalOrderList, ",");
        String newOrderAllList = StringUtils.join(finshOrderList, ",");
        OrderReportVO reportVO = OrderReportVO.builder()
                .dateList(dateListAll)
                .orderCountList(allOrderList)
                .validOrderCountList(newOrderAllList)
                .totalOrderCount(total)
                .validOrderCount(finsh)
                .orderCompletionRate(completion).build();

        return reportVO;
    }

    /**
     * 销量排名Top10
     *
     * @param begin
     * @param end
     * @return
     */
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        //根据开始时间和结束时间获取每天的日期
        Map map = new HashMap<>();
        map.put("begin", LocalDateTime.of(begin, LocalTime.MIN));
        map.put("end", LocalDateTime.of(end, LocalTime.MAX));
        map.put("status", Orders.COMPLETED);
        //查询时间内菜品和销量
        List<GoodsSalesDTO> salesTop10 = orderMapper.getSalesTop10(map);
        List<String> nameList = salesTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> numberList = salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        SalesTop10ReportVO reportVO = SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList, ",")).
                numberList(StringUtils.join(numberList, ",")).build();

        return reportVO;
    }
}
